<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手机通讯录管理器</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"],
        input[type="tel"],
        textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .button-group {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        button {
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
            transition: background-color 0.3s;
            flex: 1;
        }
        #addBtn {
            background-color: #4CAF50;
            color: white;
        }
        #searchBtn {
            background-color: #2196F3;
            color: white;
        }
        #deleteBtn {
            background-color: #f44336;
            color: white;
        }
        #resetBtn {
            background-color: #ff9800;
            color: white;
        }
        button:hover {
            opacity: 0.9;
        }
        #contactsArea {
            min-height: 200px;
            resize: vertical;
        }
        .search-results {
            margin-top: 20px;
            padding: 10px;
            background-color: #f9f9f9;
            border-radius: 4px;
            border: 1px solid #eee;
        }
        .contact-item {
            display: flex;
            justify-content: space-between;
            padding: 8px 0;
            border-bottom: 1px solid #eee;
        }
        .contact-item:last-child {
            border-bottom: none;
        }
        .contact-actions {
            display: flex;
            gap: 5px;
        }
        .edit-btn {
            background-color: #ffc107;
            color: black;
            padding: 2px 8px;
            font-size: 12px;
        }
        .remove-btn {
            background-color: #e91e63;
            color: white;
            padding: 2px 8px;
            font-size: 12px;
        }
        .status-message {
            margin-top: 10px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #dff0d8;
            color: #3c763d;
            display: block;
        }
        .error {
            background-color: #f2dede;
            color: #a94442;
            display: block;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>手机通讯录管理器</h1>

        <div class="form-group">
            <label for="nameInput">姓名：</label>
            <input type="text" id="nameInput" placeholder="请输入姓名" required>
        </div>

        <div class="form-group">
            <label for="phoneInput">电话：</label>
            <input type="tel" id="phoneInput" placeholder="请输入电话号码">
        </div>

        <div class="button-group">
            <button id="addBtn">添加联系人</button>
            <button id="searchBtn">查询联系人</button>
            <button id="deleteBtn">删除联系人</button>
            <button id="resetBtn">重置表单</button>
        </div>

        <div id="statusMessage" class="status-message"></div>

        <div class="form-group">
            <label for="contactsArea">通讯录列表：</label>
            <textarea id="contactsArea" readonly></textarea>
        </div>

        <div id="searchResults" class="search-results" style="display: none;">
            <h3>查询结果：</h3>
            <div id="resultsContainer"></div>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            // 初始化通讯录
            let contacts = JSON.parse(localStorage.getItem('phoneContacts') || '[]');
            let editingId = null;

            // 显示状态消息
            function showStatus(message, isSuccess) {
                const statusEl = $('#statusMessage');
                statusEl.text(message)
                       .removeClass('success error')
                       .addClass(isSuccess ? 'success' : 'error')
                       .fadeIn();

                setTimeout(() => {
                    statusEl.fadeOut();
                }, 3000);
            }

            // 更新通讯录显示
            function updateContactsDisplay() {
                const contactsText = contacts.map(contact =>
                    `${contact.name}：${contact.phone}`
                ).join('\n');

                $('#contactsArea').val(contactsText);
                localStorage.setItem('phoneContacts', JSON.stringify(contacts));
            }

            // 显示搜索结果
            function displaySearchResults(results) {
                const resultsContainer = $('#resultsContainer');
                resultsContainer.empty();

                if (results.length === 0) {
                    resultsContainer.append('<p>未找到匹配的联系人</p>');
                    return;
                }

                results.forEach(contact => {
                    const contactEl = $(`
                        <div class="contact-item">
                            <div>
                                <strong>${contact.name}</strong>: ${contact.phone}
                            </div>
                            <div class="contact-actions">
                                <button class="edit-btn" data-id="${contact.id}">编辑</button>
                                <button class="remove-btn" data-id="${contact.id}">删除</button>
                            </div>
                        </div>
                    `);

                    resultsContainer.append(contactEl);
                });

                $('#searchResults').show();
            }

            // 生成唯一ID
            function generateId() {
                return Date.now().toString(36) + Math.random().toString(36).substr(2);
            }

            // 添加联系人
            $('#addBtn').click(function() {
                const name = $('#nameInput').val().trim();
                const phone = $('#phoneInput').val().trim();

                if (!name) {
                    showStatus('请输入姓名', false);
                    return;
                }

                if (!phone) {
                    showStatus('请输入电话号码', false);
                    return;
                }

                if (editingId) {
                    // 更新现有联系人
                    const index = contacts.findIndex(c => c.id === editingId);
                    if (index !== -1) {
                        contacts[index] = { id: editingId, name, phone };
                        showStatus('联系人已更新', true);
                    }
                    editingId = null;
                    $('#addBtn').text('添加联系人');
                } else {
                    // 添加新联系人
                    const newContact = {
                        id: generateId(),
                        name,
                        phone
                    };

                    contacts.push(newContact);
                    showStatus('联系人已添加', true);
                }

                updateContactsDisplay();
                $('#nameInput, #phoneInput').val('');
                $('#searchResults').hide();
            });

            // 查询联系人
            $('#searchBtn').click(function() {
                const searchTerm = $('#nameInput').val().trim();

                if (!searchTerm) {
                    showStatus('请输入要查询的姓名', false);
                    return;
                }

                const results = contacts.filter(contact =>
                    contact.name.includes(searchTerm)
                );

                displaySearchResults(results);
                showStatus(`找到 ${results.length} 条匹配结果`, true);
            });

            // 删除联系人
            $('#deleteBtn').click(function() {
                const name = $('#nameInput').val().trim();

                if (!name) {
                    showStatus('请输入要删除的联系人姓名', false);
                    return;
                }

                const initialCount = contacts.length;
                contacts = contacts.filter(contact => contact.name !== name);

                if (contacts.length < initialCount) {
                    showStatus('联系人已删除', true);
                } else {
                    showStatus('未找到匹配的联系人', false);
                }

                updateContactsDisplay();
                $('#nameInput, #phoneInput').val('');
                $('#searchResults').hide();
            });

            // 重置表单
            $('#resetBtn').click(function() {
                $('#nameInput, #phoneInput').val('');
                $('#searchResults').hide();
                editingId = null;
                $('#addBtn').text('添加联系人');
            });

            // 编辑联系人
            $(document).on('click', '.edit-btn', function() {
                const contactId = $(this).data('id');
                const contact = contacts.find(c => c.id === contactId);

                if (contact) {
                    $('#nameInput').val(contact.name);
                    $('#phoneInput').val(contact.phone);
                    editingId = contact.id;
                    $('#addBtn').text('更新联系人');
                    $('html, body').animate({
                        scrollTop: 0
                    }, 500);
                }
            });

            // 删除单个联系人
            $(document).on('click', '.remove-btn', function() {
                const contactId = $(this).data('id');
                contacts = contacts.filter(c => c.id !== contactId);

                updateContactsDisplay();
                displaySearchResults(contacts.filter(c => c.id !== contactId));
                showStatus('联系人已删除', true);
            });

            // 初始化显示
            updateContactsDisplay();
        });
    </script>
</body>
</html>